'use strict';
const mongoose = require('mongoose');

module.exports={
    async list(options){
        let GameUserModel=mongoose.models['GameUser'];
        let query={};
        if(!!options.id){
            query.id=options.id;
        }
        if(!!options.nickname){
            query.nickname=options.nickname;
        }
        if(!!options.sex){
            query.sex=options.sex;
        }
        let page=1;
        let rows=20;
        if(!!options.page){
            page=parseInt(options.page);
        }
        if(!!options.rows){
            rows=parseInt(options.rows);
        }
        let gameUsers=await GameUserModel.find(query).skip((page-1)*rows).limit(rows);
        let data=[];
        gameUsers.forEach(user=>{
            data.push(user.getAdminListMessage());
        });
        let totalCount=await GameUserModel.countDocuments(query);
        return {data:data,totalCount:totalCount};
    },
    async edit(options){
        let GameUserModel=mongoose.models['GameUser'];
        let gameUser=await GameUserModel.findOne({_id:options._id});
        if(!gameUser){
            throw new Error('没有这个用户');
        }
        gameUser.nickname=options.nickname;
        gameUser.headImgUrl=options.headImgUrl;
        gameUser.sex=options.sex;
        gameUser.ppt.role=parseInt(options.role);
        gameUser.store.goldCoin=parseInt(options.goldCoin);
        gameUser.store.diamond=parseInt(options.diamond);
        await gameUser.save();
    }
};